const {Model, DataTypes} = require('sequelize');
const bcrypt = require('bcryptjs');
const sequelize = require('../../core/db');

class User extends Model {
}

User.init({
  id: {
    type: DataTypes.INTEGER.UNSIGNED
    , primaryKey: true
    , autoIncrement: true
  }
  , username: {
    type: DataTypes.STRING
    , allowNull: false
    , unique: true
  }
  , password: {
    type: DataTypes.STRING
    , allowNull: false
    , set(val) {
      const salt = bcrypt.genSaltSync(10/*花费的时间成本,数值越大安全性越高*/);
      const psw = bcrypt.hashSync(val, salt);
      /*this指代的是User实例*/
      this.setDataValue('password', psw);
    }
  }
  , status: {
    type: DataTypes.BOOLEAN
    , allowNull: false
    , defaultValue: true
  }
  , phone: {
    type: DataTypes.STRING
    , unique: true
  }
  , email: {
    type: DataTypes.STRING
    , unique: true
  }
  , nickname: {
    type: DataTypes.STRING
    , unique: true
  }
  , remark: {
    type: DataTypes.STRING
  }
}, {
  sequelize
  , tableName: 'user'
});

module.exports = User;
